//
//  VVEmoticonToolBar.swift
//  Weibo
//
//  Created by 王惠平 on 2017/4/7.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit

//表情类型

enum VVEmoticonType: Int {
    
    //最近
    case recent = 10
    
    //默认
    case normal = 11
    
    //emoji
    case emoji = 12
    
    //浪小花
    case lxh = 13
}

class VVEmoticonToolBar: UIStackView {

    //记录上一次点击的按钮
    var lastSelectedButton: UIButton?
    
    //回调点击按钮的枚举类型
    var didSelectedEmoticonClosure: ((VVEmoticonType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //设置水平方向
        self.axis = .horizontal
        
        //子按钮等比填充
        self.distribution = .fillEqually
    
        addChildButton(bgImageName: "compose_emotion_table_left", title: "最近", type: .recent)
        addChildButton(bgImageName: "compose_emotion_table_mid", title: "默认", type: .normal)
        addChildButton(bgImageName: "compose_emotion_table_mid", title: "Emoji", type: .emoji)
        addChildButton(bgImageName: "compose_emotion_table_right", title: "浪小花", type: .lxh)
    
    }
    
    //添加子控件
    fileprivate func addChildButton(bgImageName: String, title: String, type: VVEmoticonType) {
        
        let button = UIButton()
        
        //使用枚举类型的原始值作为tag
        button.tag = type.rawValue
        
        //添加点击事件
        button.addTarget(self, action: #selector(buttonAction(btn:)), for: .touchUpInside)
        
        //设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: bgImageName + "_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named: bgImageName + "_selected"), for: .selected)
        
        //设置文字
        button.setTitle(title, for: .normal)
        
        //设置文字颜色
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .selected)
        
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        //取消高亮
        button.adjustsImageWhenHighlighted = false
        
        //添加子按钮
        addArrangedSubview(button)
        
        //默认表情选中
        if type == .normal {
         
            lastSelectedButton?.isSelected = false
            
            button.isSelected = true
            
            lastSelectedButton = button
            
        }
       
    }
    
    @objc fileprivate func buttonAction(btn: UIButton) {
        
        //如果上次选中的按钮和这次的一样,不让其选中
        if lastSelectedButton == btn {
            
            return
        }
        
        //上一次的按钮取消选中
        lastSelectedButton?.isSelected = false
        
        //点击按选中
        btn.isSelected = true
        
        //记录选中的按钮
        lastSelectedButton = btn
        
        //根据tag值获取枚举
        let type = VVEmoticonType(rawValue: btn.tag)!
        
        didSelectedEmoticonClosure?(type)
    }
    
    //根据组的索引选中对应的按钮
    func selectButton(section: Int) {
        
        //viewWithTag 获取按钮的时候子控件tag不能设置为0,因为0获取的是当前控件自己(HMEmoticonToolBar)

        let button = viewWithTag(section + 10) as! UIButton
        
        //如果选中按钮和上一次的按钮一样不让其再次选中
        if button == lastSelectedButton {
            
            return
        }
        
        //上一次的按钮取消选中
        lastSelectedButton?.isSelected = false
        
        //这次的按钮选中
        button.isSelected = true
        
        //记录选中的按钮
        lastSelectedButton = button
        
    }

}
